<template>
  <el-dialog :title="titleParkProt" append-to-body :close-on-click-modal="false" @close="closeDialog()"
    v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="150px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="集团编码" prop="deptName">
            <el-tree :data="deptList" :props="defaultProps" @node-click="handleNodeClick"></el-tree>

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="集团编码" prop="deptName">
            <el-input v-model="dataForm.deptName" placeholder="集团编码">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <!-- <el-form-item label="所在停车场" prop="parkId">
            <el-input v-model="dataForm.parkId" placeholder="所在停车场"></el-input>
          </el-form-item> -->
          <el-form-item label="所在停车场" prop="parkId">
            <el-select v-model="dataForm.parkId" class="m-2" placeholder="所在停车场" style="width:100%;">
              <el-option v-for="item in parkName" :key="item.id" :label="item.parkName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="进出口名称" prop="portName">
            <el-input v-model="dataForm.portName" placeholder="进出口名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="摄像头ip" prop="portIp">
            <el-input v-model="dataForm.portIp" placeholder="进出口的IP地址:拍照摄像头ip"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用功能类别" prop="portFunType">
            <!-- <el-select v-model="dataForm.portFunType" class="m-2" placeholder="使用功能类别" style="width:100%;">
              <el-option v-for="item in inOut" :key="item.value" :label="item.label" :value="item.value" />
            </el-select> -->
            <el-radio-group v-model="dataForm.portFunType">
              <el-radio label="0" border>出口</el-radio>
              <el-radio label="1" border>入口</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否为主闸机" prop="isMain">
            <!-- <el-select v-model="dataForm.isMain" class="m-2" placeholder="是否为主闸机" style="width:100%;">
              <el-option v-for="item in wether" :key="item.value" :label="item.label" :value="item.value" />
            </el-select> -->
            <el-radio-group v-model="dataForm.isMain">
              <el-radio label="0" border>否</el-radio>
              <el-radio label="1" border>是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否仅月租" prop="isOnlyMonthly">
            <!-- <el-select v-model="dataForm.isOnlyMonthly" class="m-2" placeholder="是否仅月租" style="width:100%;">
              <el-option v-for="item in wether" :key="item.value" :label="item.label" :value="item.value" />
            </el-select> -->
            <el-radio-group v-model="dataForm.isOnlyMonthly">
              <el-radio label="0" border>否</el-radio>
              <el-radio label="1" border>是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否收费" prop="isMastFee">
            <!-- <el-select v-model="dataForm.isMastFee" class="m-2" placeholder="0 可以不收费 1必须付费才能过去" style="width:100%;">
              <el-option v-for="item in wether" :key="item.value" :label="item.label" :value="item.value" />
            </el-select> -->
            <el-radio-group v-model="dataForm.isMastFee">
              <el-radio label="0" border>否</el-radio>
              <el-radio label="1" border>是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12"></el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="描述" prop="description">
            <el-input v-model="dataForm.description" placeholder="描述"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="位置描述" prop="realLocation">
            <el-input v-model="dataForm.realLocation" placeholder="描述"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="出入口图片" prop="img">
            <imageUpload v-model="dataForm.img"></imageUpload>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { fetchListTwo } from '@/api/stop/parking'
import { fetchTree } from '@/api/admin/dept'
import { getObj, addObj, putObj } from '@/api/stop/parkingport'

export default {
  data () {
    return {
      visible: false,
      canSubmit: false,
      queryParams: {
        // current: 1,
        // size: 10,
      },
      dataForm: {

      },
      dataRule: {
        deptName: [
          { required: true, message: '集团编码不能为空', trigger: 'blur' }
        ],
        parkId: [
          { required: true, message: '所在停车场不能为空', trigger: 'blur' }
        ],

        portIp: [
          { required: true, message: '进出口的IP地址:拍照摄像头ip不能为空', trigger: 'blur' }
        ],


        portFunType: [
          { required: true, message: '使用功能类别(0:停车场内;1:停车场入口;2:停车场出口)不能为空', trigger: 'blur' }
        ],

        controlType: [
          { required: true, message: '控制类型不能为空', trigger: 'blur' }
        ],

        // description: [
        //   { required: true, message: '描述不能为空', trigger: 'blur' }
        // ],

        portName: [
          { required: true, message: '进出口名称不能为空', trigger: 'blur' }
        ],

        isMain: [
          { required: true, message: '(1-主闸机, 0-副闸机)来自于字典表不能为空', trigger: 'blur' }
        ],

        cameraPort: [
          { required: true, message: '摄像头端口不能为空', trigger: 'blur' }
        ],

        isSync: [
          { required: true, message: '0 未下发 1 已下发不能为空', trigger: 'blur' }
        ],

        deptId: [
          { required: true, message: '集团编码不能为空', trigger: 'blur' }
        ],

        isOnlyMonthly: [
          { required: true, message: '是否仅月租不能为空', trigger: 'blur' }
        ],

        isMastFee: [
          { required: true, message: '0 可以不收费 1必须付费才能过去不能为空', trigger: 'blur' }
        ],





      },
      wether: [
        { value: 0, label: '否' },
        { value: 1, label: '是' },
      ],
      inOut: [
        { value: 0, label: '出口' },
        { value: 1, label: '入口' },
      ],
      parkName: [],
      titleParkProt: '',
      deptList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  mounted () {

  },
  methods: {
    getList () {
      fetchTree().then(response => {
        // console.log(response);
        this.deptList = response.data.data
      })
    },
    getDataList () {
      this.dataListLoading = true
      fetchListTwo().then(response => {
        this.parkName = response.data.data
      })
    },
    handleNodeClick (data) {
      this.dataForm.deptId = data.id
      this.dataForm.deptName = data.name
    },
    init (id) {
      this.getList()
      this.getDataList()
      if (id == undefined) {
        this.titleParkProt = '新增'
      } else {
        this.titleParkProt = '修改'
      }
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (id) {
          getObj(id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //重置表单
    closeDialog () {
      this.$refs["dataForm"].resetFields()
    }
  }
}
</script>
